# -*- coding: utf-8 -*-
import scrapy
import re
from scrapy.selector import HtmlXPathSelector
class doc_spider(scrapy.spiders.Spider):
    name = 'doc'
    allowed_domain = ['tieba.baidu.com']
    start_urls = {
      'http://tieba.baidu.com/p/4938211916?pn=1&statsInfo=frs_pager'
    }

    def parse(self, response):
        #获取页码数
        for sel in response.xpath('//*[@id="thread_theme_5"]/div[1]/ul/li[@class="l_pager pager_theme_4 pb_list_pager"]/a'):
            data=sel.xpath('@href').extract()
            name=sel.xpath('text()').extract()[0].encode('utf-8')
            if name=="尾页":
                last=int(re.split('=|\'',str(data))[-2])
            else:
                last = -1

        currentnum = int(re.split('\'',str(response.xpath('//*[@id="thread_theme_5"]/div[1]/ul/li[@class="l_pager pager_theme_4 pb_list_pager"]/span/text()').extract()))[-2])
        print currentnum,last


        if currentnum<=last:
            url = 'http://tieba.baidu.com/p/4938211916?pn=%s&statsInfo=frs_pager' % str(currentnum+1)
            print "xxxxx"
            print url
            self.start_urls = url
            yield scrapy.Request(url,callback=self.parse)

        for sel in response.xpath('//div[@class="d_post_content j_d_post_content  clearfix"]'):
            desc = sel.xpath('text()').extract()[0].encode('utf-8')
            print currentnum, desc




